package com.avcdata.national.utils;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.avcdata.aoscustommade.R;

public class DrawCircle extends View {

    public Context context;
    public int dp2px;
    public String persent;
    public String persentyear;

    public DrawCircle(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public DrawCircle(Context context, int dp2px, String persent) {
        super(context);
        this.dp2px = dp2px;
        this.persent = persent;
    }

    public DrawCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建画笔
        Paint p = new Paint();
        //抗锯齿
        p.setAntiAlias(true);
        int Xcenter = getWidth() / 2;
        int Ycenter = getHeight() / 2;
        int mRadius = 50 * dp2px;
        int circleWidth = 6 * dp2px;

        //外圆
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(circleWidth);
        RectF oval = new RectF();                       //RectF对象
        oval.left = Xcenter - mRadius;                      //左边
        oval.top = Ycenter - mRadius;                       //上边
        oval.right = Xcenter + mRadius;                      //右边
        oval.bottom = Ycenter + mRadius;                     //下边
        p.setColor(getResources().getColor(R.color.background_gray));
        canvas.drawArc(oval,270,360,false,p);
        p.setColor(getResources().getColor(R.color.ThemeColor));

        try {
            canvas.drawArc(oval, 270, (360 * Float.parseFloat(persent) / 100), false, p);    //绘制圆弧
        }catch(Exception e){
            canvas.drawArc(oval, 270, 0, false, p);    //绘制圆弧
        }

        //圆心内的标题
        p.setStrokeWidth(1 * dp2px);
        p.setTextSize(16 * dp2px);
        p.setColor(getResources().getColor(R.color.ThemeColor));
        try {
            canvas.drawText(MyUtils.format2(Float.parseFloat(persent))+"%", Xcenter - 24 * dp2px, Ycenter + 6 * dp2px, p);
        }catch (Exception e){
            canvas.drawText("null", Xcenter - 24 * dp2px, Ycenter + 6 * dp2px, p);
        }

    }
}

